﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Xml.Serialization;

public class ComboEditor : EditorWindow
{

    #region Private Fields
    private string defFilePath;

    private ComboDefineList _comboList;
    private string[] _comboTextList;
    private int _selectedComboIndex;
    #endregion 

    public ComboEditor()
    {
        // Put together filepath
        defFilePath = Path.Combine(Application.dataPath, ComboDefine.DEFINE_FILE);

        Stream fs;
        XmlSerializer serializer = new XmlSerializer(typeof(ComboDefineList));

        // Create define-file if it doesn't exist already
        if (!File.Exists(defFilePath))
        {
            // Create a new file
            fs = new FileStream(defFilePath, FileMode.OpenOrCreate);
            _comboList = new ComboDefineList();
            serializer.Serialize(fs, _comboList);
            fs.Close();
        }

        // Deserialize contents
        fs = new FileStream(defFilePath, FileMode.Open);
        _comboList = serializer.Deserialize(fs) as ComboDefineList;
        RefreshComboTextList();
        fs.Close();
        
    }

    [MenuItem("Baiberu/Combo Editor")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        ComboEditor window = (ComboEditor)EditorWindow.GetWindow(typeof(ComboEditor));
    }

    private void RefreshComboTextList()
    {
        _comboTextList = _comboList.ToStringArray();
    }

    private void SaveData()
    {
        Stream fs;
        XmlSerializer serializer = new XmlSerializer(typeof(ComboDefineList));

        // Create a new file
        fs = new FileStream(defFilePath, FileMode.OpenOrCreate);
        serializer.Serialize(fs, _comboList);
        fs.Close();
    }

    void OnGUI()
    {
        float windowWidth = position.width - 15;

        // Deffile Heading
        GUILayout.Label("Definition File", EditorStyles.boldLabel, GUILayout.Width(200));
        if(File.Exists(defFilePath))
        {
            // File exists
            if (GUILayout.Button(defFilePath, GUILayout.Width(400)))
            {
                // Launch file
                System.Diagnostics.Process.Start(defFilePath);
            }

            // Combos
            GUILayout.Label("Contents", EditorStyles.boldLabel, GUILayout.Width(200));

            GUILayout.BeginHorizontal();
            {
                if (GUILayout.Button("Add Combo", GUILayout.Width(100)))
                {
                    _comboList.AddNew("New Combo #" + (_comboList.Items.Count + 1));
                    RefreshComboTextList();
                }

                if (GUILayout.Button("Delete Combo", GUILayout.Width(100)) && _comboList.Items.Count > 0)
                {
                    _comboList.Items.RemoveAt(_selectedComboIndex);
                    RefreshComboTextList();
                }

                GUILayout.Label("|", GUILayout.Width(10));

                if (GUILayout.Button("Save All", GUILayout.Width(100)))
                {
                    SaveData();
                    RefreshComboTextList();
                }
            }
            GUILayout.EndHorizontal();

            // Reset index to avoid errors when the user deletes all 
            _selectedComboIndex = 0;

            // Combolist
            if (_comboTextList != null && _comboList.Items.Count > 0)
            {
                GUILayout.BeginVertical("box");
                {
                    _selectedComboIndex = GUILayout.Toolbar(_selectedComboIndex, _comboTextList, GUILayout.Width(Mathf.Min(200 * _comboTextList.Length, windowWidth)));
                    
                    // Combobuttons-Toolbar
                    GUILayout.BeginHorizontal();
                    {
                        if (GUILayout.Button("←", GUILayout.Width(25)))
                        {
                            _comboList.Items[_selectedComboIndex].AddNewComboKey(InputKey.Left);
                        }

                        if (GUILayout.Button("↑", GUILayout.Width(25)))
                        {
                            _comboList.Items[_selectedComboIndex].AddNewComboKey(InputKey.Jump);
                        }

                        if (GUILayout.Button("→", GUILayout.Width(25)))
                        {
                            _comboList.Items[_selectedComboIndex].AddNewComboKey(InputKey.Right);
                        }

                        if (GUILayout.Button("↓", GUILayout.Width(25)))
                        {
                            _comboList.Items[_selectedComboIndex].AddNewComboKey(InputKey.Crouch);
                        }

                        if (GUILayout.Button("A", GUILayout.Width(25)))
                        {
                            _comboList.Items[_selectedComboIndex].AddNewComboKey(InputKey.AttackA);
                        }

                        if (GUILayout.Button("B", GUILayout.Width(25)))
                        {
                            _comboList.Items[_selectedComboIndex].AddNewComboKey(InputKey.AttackA);
                        }

                        GUILayout.Label("|", GUILayout.Width(10));

                        if (GUILayout.Button("Clear", GUILayout.Width(45)))
                        {
                            _comboList.Items[_selectedComboIndex].Combokeys.Clear();
                        }

                        if (GUILayout.Button("<<", GUILayout.Width(45)) && _comboList.Items[_selectedComboIndex].Combokeys.Count > 0)
                        {
                            _comboList.Items[_selectedComboIndex].Combokeys.RemoveAt(_comboList.Items[_selectedComboIndex].Combokeys.Count - 1);
                        }
                    }
                    GUILayout.EndHorizontal();

                    // Description
                    GUILayout.Label("", GUILayout.Height(5));
                    GUILayout.BeginHorizontal();
                    {
                        GUILayout.Label("Description", GUILayout.Width(200));
                        _comboList.Items[_selectedComboIndex].Description = GUILayout.TextField(_comboList.Items[_selectedComboIndex].Description, GUILayout.Width(200));
                    }
                    GUILayout.EndHorizontal();

                    GUILayout.Label("Combo keys");
                    GUILayout.BeginHorizontal("box", GUILayout.Width(windowWidth));
                    {
                        // List current combokeys
                        foreach (InputKey ik in _comboList.Items[_selectedComboIndex].Combokeys)
                        {
                            GUILayout.Button(ik.ToString(), GUILayout.Width(40), GUILayout.Height(40));
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndVertical();
            }

        }
        else
        {
            EditorGUILayout.HelpBox("Definition File not found!", MessageType.Error);
        }
    }


}
